package h0801;

import java.util.*;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 下一个更大元素
 * @date 2024/8/1 19:10
 */
public class NextLargerFactor {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] array = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int[] ints = nextGreaterElements(array);
        System.out.println(Arrays.toString(ints));
    }

    //TODO 自己的方法好理解一点
    public static int[] nextGreaterElements(int[] nums) {
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int temp = -1;
            for (int j = i + 1; j < nums.length + i; j++) {
                int newJ = j % (nums.length);
                if (i ==newJ) continue;
                if (nums[newJ] > nums[i]) {
                    temp = nums[newJ];
                    break;
                }
            }
            res.add(temp);
        }
        return res.stream().mapToInt(Integer::intValue).toArray();
    }
    public static int[] nextGreaterElements2(int[] nums){
        int n=nums.length;
        int[] res=new int[n];
        Arrays.fill(res,-1); //初始化结果数组为-1
        Deque<Integer> stack=new ArrayDeque<>();

        for (int i = 0; i < 2*n; i++) {
            int num=nums[i%n];
            while (!stack.isEmpty()&&nums[stack.peek()]<num){
                res[stack.pop()]=num;
            }
            if (i<n){
                stack.push(i);
            }
        }
        return res;
    }
}
